﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;

public class Function
{
    public static void alert(string str, bool isEnd)
    {
        string s = string.Format("<script language=\"JavaScript\" type=\"text/javascript\" >alert(\"{0}\");</script>", str);
        HttpContext.Current.Response.Write(s);
        if (isEnd)
        {
            HttpContext.Current.Response.End();
        }
    }

    public static string ChildIdList(DataTable dataTable, string idField, string parentIdField, string parentId, bool fistBool)
    {
        string str = "";
        if (fistBool)
        {
            str = string.Format("{0}", parentId);
        }
        string filterExpression = string.Format("{0}={1}", parentIdField, parentId);
        DataRow[] rowArray = dataTable.Select(filterExpression);
        if (rowArray.Length > 0)
        {
            for (int i = 0; i < rowArray.Length; i++)
            {
                DataRow row = rowArray[i];
                string str3 = row[idField].ToString();
                str = string.Format("{0},{1}", str, str3) + ChildIdList(dataTable, idField, parentIdField, str3, false);
            }
        }
        return str;
    }

    public static string GetIP()
    {
        return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
    }

    public static string GetURL()
    {
        return HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString();
    }

    public static string MD5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(string.Format("ShanRui{0} By {0} Lspcieee", str), "MD5");
    }

    public static void PopulateManagerTreeNode(DataTable inputTable, TreeNode inputMenuItem, string textField, string idField, string parentIdField, int parentId, string parameterField, string attribute, string roleList)
    {
        string filterExpression = string.Format("{0}={1}", parentIdField, parentId);
        DataRow[] rowArray = inputTable.Select(filterExpression);
        if (rowArray.Length > 0)
        {
            for (int i = 0; i < rowArray.Length; i++)
            {
                DataRow row = rowArray[i];
                TreeNode child = new TreeNode();
                child.Text = (string) row[textField];
                if (Website.IsURLRewriter())
                {
                    child.NavigateUrl = string.Format("~/{1}-{0}_{2}.aspx", attribute, (string) row[parameterField], Website.GetLanguage());
                }
                else
                {
                    child.NavigateUrl = string.Format("~/{0}.aspx?FileName={1}&Language={2}", MenuProvider.FormatMenuUrl(attribute), (string) row[parameterField], Website.GetLanguage());
                }
                child.Target = "_blank";
                string str2 = ((int) row["MenuId"]).ToString();
                string str3 = string.Format("M{2}_{0}_{1}", attribute, ((int) row[idField]).ToString(), str2);
                child.Value = str3;
                child.Checked = roleList.Contains(str3 + ",") || roleList.Contains(str3 + "_");
                child.Expand();
                string str5 = "";
                string str6 = "";
                string str7 = "";
                string str8 = "";
                string str9 = "";
                string str10 = "";
                string str11 = "";
                string str12 = "";
                if (roleList.Contains(str3 + "_a"))
                {
                    str5 = "checked=\"checked\"";
                }
                if (roleList.Contains(str3 + "_b"))
                {
                    str6 = "checked=\"checked\"";
                }
                if (roleList.Contains(str3 + "_c"))
                {
                    str7 = "checked=\"checked\"";
                }
                if (roleList.Contains(str3 + "_d"))
                {
                    str8 = "checked=\"checked\"";
                }
                if (roleList.Contains(str3 + "_e"))
                {
                    str9 = "checked=\"checked\"";
                }
                if (roleList.Contains(str3 + "_f"))
                {
                    str10 = "checked=\"checked\"";
                }
                if (roleList.Contains("m" + str2 + "_g"))
                {
                    str11 = "checked=\"checked\"";
                }
                if (roleList.Contains("m" + str2 + "_h"))
                {
                    str12 = "checked=\"checked\"";
                }
                string str13 = attribute;
                if (str13 == null)
                {
                    goto Label_03FD;
                }
                if (!(str13 == "0"))
                {
                    if (str13 == "1")
                    {
                        goto Label_02E7;
                    }
                    if (str13 == "2")
                    {
                        goto Label_032C;
                    }
                    if (str13 == "3")
                    {
                        goto Label_0371;
                    }
                    if (str13 == "4")
                    {
                        goto Label_03C7;
                    }
                    goto Label_03FD;
                }
                string str4 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />编辑栏目&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />删除栏目&nbsp;<input type=\"checkbox\" name=\"checkbox3\" value=\"{0}_c\" {3} />栏目内容&nbsp;<input type=\"checkbox\" name=\"checkbox4\" value=\"{0}_d\" {4} />添子栏目</span>", new object[] { str3, str5, str6, str7, str8 });
                goto Label_0404;
            Label_02E7:;
                str4 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />编辑文章&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />删除文章&nbsp;<input type=\"checkbox\" name=\"checkbox3\" value=\"{0}_c\" {3} />添加文章&nbsp;<input type=\"checkbox\" name=\"checkbox4\" value=\"{0}_d\" {4} />编辑分类&nbsp;<input type=\"checkbox\" name=\"checkbox5\" value=\"{0}_e\" {5} />删除分类&nbsp;<input type=\"checkbox\" name=\"checkbox6\" value=\"{0}_f\" {6} />添子分类</span>", new object[] { str3, str5, str6, str7, str8, str9, str10 });
                goto Label_0404;
            Label_032C:;
                str4 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />编辑图文&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />删除图文&nbsp;<input type=\"checkbox\" name=\"checkbox3\" value=\"{0}_c\" {3} />添加图文&nbsp;<input type=\"checkbox\" name=\"checkbox4\" value=\"{0}_d\" {4} />编辑分类&nbsp;<input type=\"checkbox\" name=\"checkbox5\" value=\"{0}_e\" {5} />删除分类&nbsp;<input type=\"checkbox\" name=\"checkbox6\" value=\"{0}_f\" {6} />添子分类</span>", new object[] { str3, str5, str6, str7, str8, str9, str10 });
                goto Label_0404;
            Label_0371:;
                str4 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox8\" value=\"m{9}_h\" {8} />栏目配置&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />编辑产品&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />删除产品&nbsp;<input type=\"checkbox\" name=\"checkbox3\" value=\"{0}_c\" {3} />添加产品&nbsp;<input type=\"checkbox\" name=\"checkbox4\" value=\"{0}_d\" {4} />编辑分类&nbsp;<input type=\"checkbox\" name=\"checkbox5\" value=\"{0}_e\" {5} />删除分类&nbsp;<input type=\"checkbox\" name=\"checkbox6\" value=\"{0}_f\" {6} />添子分类&nbsp;<input type=\"checkbox\" name=\"checkbox7\" value=\"m{9}_g\" {7} />订单管理&nbsp;</span>", new object[] { str3, str5, str6, str7, str8, str9, str10, str11, str12, str2 });
                goto Label_0404;
            Label_03C7:;
                str4 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />删除留言&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />回复留言&nbsp;<input type=\"checkbox\" name=\"checkbox3\" value=\"{0}_c\" {3} />审核留言&nbsp;<input type=\"checkbox\" name=\"checkbox4\" value=\"{0}_d\" {4} />留言配置</span>", new object[] { str3, str5, str6, str7, str8 });
                goto Label_0404;
            Label_03FD:
                str4 = "";
            Label_0404:
                child.Text = child.Text + str4;
                child.ToolTip = "点击访问前台页面";
                inputMenuItem.ChildNodes.Add(child);
                int num2 = (int) row[idField];
                PopulateManagerTreeNode(inputTable, inputMenuItem.ChildNodes[i], textField, idField, parentIdField, num2, parameterField, attribute, roleList);
            }
        }
    }

    public static void PopulateMenuItem(DataTable inputTable, MenuItem inputMenuItem, string textField, string idField, string parentIdField, int parentId, string parameterField, string attribute)
    {
        string filterExpression = string.Format("{0}={1}", parentIdField, parentId);
        DataRow[] rowArray = inputTable.Select(filterExpression);
        if (rowArray.Length > 0)
        {
            for (int i = 0; i < rowArray.Length; i++)
            {
                DataRow row = rowArray[i];
                MenuItem child = new MenuItem();
                child.Text = (string) row[textField];
                if (Website.IsURLRewriter())
                {
                    child.NavigateUrl = string.Format("~/{1}-{0}_{2}.aspx", attribute, (string) row[parameterField], Website.GetLanguage());
                }
                else
                {
                    child.NavigateUrl = string.Format("~/{0}.aspx?FileName={1}&Language={2}", MenuProvider.FormatMenuUrl(attribute), (string) row[parameterField], Website.GetLanguage());
                }
                inputMenuItem.ChildItems.Add(child);
                int num2 = (int) row[idField];
                PopulateMenuItem(inputTable, inputMenuItem.ChildItems[i], textField, idField, parentIdField, num2, parameterField, attribute);
            }
        }
    }

    public static string PopulateSiteMapPath(DataTable inputTable, string textField, string idField, string parentIdField, string childId, string parameterField, string attribute)
    {
        string str2;
        string filterExpression = string.Format("{0}={1}", idField, childId);
        DataRow[] rowArray = inputTable.Select(filterExpression);
        if (rowArray.Length <= 0)
        {
            return "";
        }
        DataRow row = rowArray[0];
        string str = ((int) row[parentIdField]).ToString();
        string str4 = VirtualPathUtility.ToAbsolute("~/");
        if (Website.IsURLRewriter())
        {
            str2 = string.Format("&gt;<a href=\"{4}{1}-{0}_{2}.aspx\">{3}</a>", new object[] { attribute, row[parameterField].ToString(), Website.GetLanguage(), (string) row[textField], str4 });
        }
        else
        {
            str2 = string.Format("&gt;<a href=\"{4}{0}.aspx?FileName={1}&Language={2}\">{3}</a>", new object[] { MenuProvider.FormatMenuUrl(attribute), row[parameterField].ToString(), Website.GetLanguage(), (string) row[textField], str4 });
        }
        string str5 = str;
        return (PopulateSiteMapPath(inputTable, textField, idField, parentIdField, str5, parameterField, attribute) + str2);
    }

    public static void PopulateTable(DataTable oldTable, DataTable newTable, string textField, string idField, string parentIdField, int parentId, int depth)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < depth; i++)
        {
            if (i == (depth - 1))
            {
                builder.Append("┠");
            }
            else
            {
                builder.Append("　");
            }
        }
        depth++;
        string filterExpression = string.Format("{0}={1}", parentIdField, parentId);
        DataRow[] rowArray = oldTable.Select(filterExpression);
        if (rowArray.Length > 0)
        {
            for (int j = 0; j < rowArray.Length; j++)
            {
                DataRow row = rowArray[j];
                row.BeginEdit();
                row[textField + "Text"] = builder.ToString() + ((string) row[textField]);
                row.EndEdit();
                newTable.ImportRow(row);
                int num3 = (int) row[idField];
                PopulateTable(oldTable, newTable, textField, idField, parentIdField, num3, depth);
            }
        }
    }

    public static void PopulateTreeNode(DataTable inputTable, TreeNode inputMenuItem, string textField, string idField, string parentIdField, int parentId, string parameterField, string attribute, string roleList)
    {
        string filterExpression = string.Format("{0}={1}", parentIdField, parentId);
        DataRow[] rowArray = inputTable.Select(filterExpression);
        if (rowArray.Length > 0)
        {
            for (int i = 0; i < rowArray.Length; i++)
            {
                DataRow row = rowArray[i];
                TreeNode child = new TreeNode();
                child.Text = (string) row[textField];
                if (Website.IsURLRewriter())
                {
                    child.NavigateUrl = string.Format("~/{1}-{0}_{2}.aspx", attribute, (string) row[parameterField], Website.GetLanguage());
                }
                else
                {
                    child.NavigateUrl = string.Format("~/{0}.aspx?FileName={1}&Language={2}", MenuProvider.FormatMenuUrl(attribute), (string) row[parameterField], Website.GetLanguage());
                }
                child.Target = "_blank";
                string str2 = string.Format("{0}_{1}", attribute, ((int) row[idField]).ToString());
                child.Value = str2;
                child.Checked = roleList.Contains(str2 + ",") || roleList.Contains(str2 + "_");
                child.Expand();
                string str4 = "";
                string str5 = "";
                string str6 = "";
                if (roleList.Contains(str2 + "_a"))
                {
                    str4 = "checked=\"checked\"";
                }
                if (roleList.Contains(str2 + "_b"))
                {
                    str5 = "checked=\"checked\"";
                }
                if (roleList.Contains(str2 + "_c"))
                {
                    str6 = "checked=\"checked\"";
                }
                string str7 = attribute;
                if (str7 == null)
                {
                    goto Label_0241;
                }
                if (!(str7 == "0"))
                {
                    if (str7 == "1")
                    {
                        goto Label_01D5;
                    }
                    if (str7 == "2")
                    {
                        goto Label_01E9;
                    }
                    if (str7 == "3")
                    {
                        goto Label_01FD;
                    }
                    if (str7 == "4")
                    {
                        goto Label_022D;
                    }
                    goto Label_0241;
                }
                string str3 = "";
                goto Label_0248;
            Label_01D5:
                str3 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />列表&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />详页</span>", str2, str4, str5);
                goto Label_0248;
            Label_01E9:
                str3 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />列表&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />详页</span>", str2, str4, str5);
                goto Label_0248;
            Label_01FD:;
                str3 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />列表&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />详页</span>", new object[] { str2, str4, str5, str6 });
                goto Label_0248;
            Label_022D:
                str3 = string.Format("<span title=\"\" onClick=\"spanClick(this,this.value)\" class=\"rightSpan\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"checkbox\" name=\"checkbox1\" value=\"{0}_a\" {1} />列表&nbsp;<input type=\"checkbox\" name=\"checkbox2\" value=\"{0}_b\" {2} />提交信息</span>", str2, str4, str5);
                goto Label_0248;
            Label_0241:
                str3 = "";
            Label_0248:
                child.Text = child.Text + str3;
                child.ToolTip = "点击访问前台页面";
                inputMenuItem.ChildNodes.Add(child);
                int num2 = (int) row[idField];
                PopulateTreeNode(inputTable, inputMenuItem.ChildNodes[i], textField, idField, parentIdField, num2, parameterField, attribute, roleList);
            }
        }
    }

    public static string RandNum(int n)
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random(DateTime.Now.Millisecond);
        for (int i = 0; i < n; i++)
        {
            builder.Append(random.Next(0, 9).ToString());
        }
        return builder.ToString();
    }
}

